package com.example.hasee.zhcd.Activities.TongJi;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.AtWorkActivity;
import com.example.hasee.zhcd.Adapters.DlfcxAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.DateTimePickDialogUtil;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class DlfcxActivity extends BaseActivity {
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private TextView title;
    private ImageView back;
    private ImageView home;
    private Spinner lxspinner;
    private Button btn_query;
    private EditText start_date;
    private EditText end_date;
    private ListView dlfcx_list;
    private int mYear;
    private int mMonth;
    private int mDay;
    private String startString;
    private String endString;
    private String strCGS;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private DlfcxAdapter adapter;
    private ArrayList<String> cgs_name_list = new ArrayList<String>();
    private ArrayList<String> cgs_code_list = new ArrayList<String>();
    private ArrayAdapter<String> cgs_adapter;
    private TextView heji;

    @Override
    public int contentViewId() {
        return R.layout.activity_dlfcx;
    }

    @Override
    public void set() {
        title.setText("代理费查询");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        start_date.setOnClickListener(this);
        end_date.setOnClickListener(this);
        lxspinner = (Spinner) findViewById(R.id.lxspinner);
        btn_query.setOnClickListener(this);
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH) - 1;
        mDay = c.get(Calendar.DAY_OF_MONTH);
        String OldDate = getTime().substring(0, 8) + "01";
        start_date.setText(OldDate);
        end_date.setText(getTime());
        startString = start_date.getText().toString();
        endString = end_date.getText().toString();
        start_date.addTextChangedListener(new DateWatcher());
        end_date.addTextChangedListener(new DateWatcher());
        getCompany();
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        btn_query = (Button) findViewById(R.id.btn_query);
        start_date = (EditText) findViewById(R.id.start_date);
        end_date = (EditText) findViewById(R.id.end_date);
        dlfcx_list = (ListView) findViewById(R.id.dlfcx_list);
        heji = (TextView) findViewById(R.id.heji);
    }

    public String getTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    private void getCompany() {
        ProgressDialogUtils.createProgressDialog(DlfcxActivity.this, "正在加载列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strKSSJ", startString);
        map.put("strJSSJ", endString);
        httpUtils.get("ZHTJ_CGS_DLF", map, "ZHTJ_CGS_DLF");
    }

    private void getDlflist() {
        ProgressDialogUtils.createProgressDialog(DlfcxActivity.this, "正在加载列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strKSSJ", startString);
        map.put("strJSSJ", endString);
        map.put("strCGS", strCGS);
        httpUtils.get("ZHTJ_DLFCX", map, "ZHTJ_DLFCX");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHTJ_CGS_DLF":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        cgs_name_list.clear();
                        cgs_code_list.clear();
                        for (int i = 0; i < data.length(); i++) {
                            cgs_name_list.add(data.getJSONObject(i).getString("fname"));
                            cgs_code_list.add(data.getJSONObject(i).getString("fcode"));
                        }
                    }
                    cgs_adapter = new ArrayAdapter<String>(DlfcxActivity.this, android.R.layout.simple_spinner_dropdown_item, cgs_name_list);
                    lxspinner.setAdapter(cgs_adapter);
                    lxspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            startString = start_date.getText().toString();
                            endString = end_date.getText().toString();
                            strCGS = cgs_code_list.get(i);
                            getDlflist();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHTJ_DLFCX":
                Log.e("ZHTJ_DLFCX", msg.getMsg());
                //ZHTJ_DLFCX: {"data": [{"fshipname":"中汇68", "fhc":"1809", "flx":"扬州海昌", "fberth":"707", "flgsj":"05-05 06:30", "fzx":"装货", "fhm":"煤炭", "fsl":"59894", "ffl":"27", "fje":"16171.38"},
                // {"fshipname":"中汇68", "fhc":"1810", "flx":"扬州海昌", "fberth":"707", "flgsj":"05-17 23:35", "fzx":"装货", "fhm":"煤炭", "fsl":"60934", "ffl":"27", "fje":"16452.18"},
                // {"fshipname":"中汇68", "fhc":"1811", "flx":"扬州海昌", "fberth":"706", "flgsj":"05-31 15:20", "fzx":"装货", "fhm":"煤炭", "fsl":"63873", "ffl":"27", "fje":"17245.71"}],"rspcode": 0,"rspmessage":"合计金额:49869.27"}
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                            map.put("fhc", data.getJSONObject(i).getString("fhc"));
                            map.put("fberth", data.getJSONObject(i).getString("fberth"));
                            map.put("flx", data.getJSONObject(i).getString("flx"));
                            map.put("fzx", data.getJSONObject(i).getString("fzx"));
                            map.put("fhm", data.getJSONObject(i).getString("fhm"));
                            map.put("flgsj", data.getJSONObject(i).getString("flgsj"));
                            map.put("fsl", data.getJSONObject(i).getString("fsl"));
                            map.put("ffl", data.getJSONObject(i).getString("ffl"));
                            map.put("fje", data.getJSONObject(i).getString("fje"));
                            list.add(map);
                        }
                        dlfcx_list.setVisibility(View.VISIBLE);
                        adapter = new DlfcxAdapter(DlfcxActivity.this, list);
                        dlfcx_list.setAdapter(adapter);
                        heji.setText(rspmessage);
                    }else{
                        dlfcx_list.setVisibility(View.GONE);
                        Toast.makeText(DlfcxActivity.this,rspmessage,Toast.LENGTH_LONG).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                GlobalParameter.JumpFrontPage(getApplicationContext());
                break;
            case R.id.start_date:
                String t1 = "";
                if (start_date.getText().toString().equals("")) {
                    t1 = getTime();
                } else {
                    t1 = start_date.getText().toString();
                }
                DateTimePickDialogUtil dialog1 = new DateTimePickDialogUtil(this,
                        t1);
                dialog1.dateTimePicKDialog(start_date);
                break;
            case R.id.end_date:
                String t2 = "";
                if (end_date.getText().toString().equals("")) {
                    t2 = getTime();
                } else {
                    t2 = end_date.getText().toString();
                }
                DateTimePickDialogUtil dialog2 = new DateTimePickDialogUtil(this,
                        t2);
                dialog2.dateTimePicKDialog(end_date);
                break;
            case R.id.btn_query:
                list.clear();
                startString = start_date.getText().toString();
                endString = end_date.getText().toString();
                View errorview = inputCheck();
                if (errorview != null) {
                    errorview.setFocusable(true);
                    errorview.requestFocus();
                    errorview.setFocusableInTouchMode(true);
                    return;
                } else {
                    getDlflist();
                }
                break;
        }
    }

    private View inputCheck() {
        if (startString.length() == 0) {
            return start_date;
        } else if (endString.length() == 0) {
            return end_date;
        }
        return null;
    }

    class DateWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            startString = start_date.getText().toString();
            endString = end_date.getText().toString();
            getCompany();
        }
    }
}
